package com.kingsoft.gtdsys.dao.impl.sqllite;

import java.util.ArrayList;
import java.util.List;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;

import com.kingsoft.gtdsys.dao.IActionDAO;
import com.kingsoft.gtdsys.entity.Action;
import com.kingsoft.gtdsys.util.GlobalValues;
import com.kingsoft.gtdsys.util.Log;

public class ActionDAO implements IActionDAO {
	public static final String TABLE_NAME = "t_actions";
	private static final String[] COLS = new String[]{ "_id", "content", "date",
		"detail", "importance", "context", "energy", "planTime", "needTime", "completed"};

	
	private SQLiteDatabase db;
	private DBOpenHelper dbOpenHelper;
	
	public ActionDAO() {
		this.dbOpenHelper = new DBOpenHelper(GlobalValues.context);
		this.establishDb();
	}
	
	private void establishDb() {
		if (db == null) {
			db = dbOpenHelper.getWritableDatabase();
			dbOpenHelper.onCreate(db);
		}
	}
	
	public void cleanup() {
		if (db != null) {
			db.close();
			db = null;
		}
	}
	
	public void insert(Action action) {
		try{
			ContentValues values = new ContentValues();
			values.put("content", action.getContent());
			values.put("date", action.getDate());
			values.put("detail", action.getDetail());
			values.put("importance", action.getImportance());
			values.put("context", action.getContent());
			values.put("energy", action.getEnergy());
			values.put("planTime", action.getPlanTime());
			values.put("needTime", action.getNeedTime());
			values.put("completed", action.getCompleted());
			db.insert(TABLE_NAME, null, values);
		} catch (Exception e) {
			Log.e(e.toString());
		}
	}
	
	@Override
	public List<Action> findAll() {
		List<Action> actions = new ArrayList<Action>();
		Cursor c = null;
		try {
			c = db.query(TABLE_NAME, COLS, null, null, null, null, null);
			int rowCount = c.getCount();
			c.moveToFirst();
			for (int i = 0; i < rowCount; i++) {
				Action action = new Action();
				action.setId(c.getInt(0));
				action.setContent(c.getString(1));
				action.setDetail(c.getString(2));
				action.setImportance(c.getString(3));
				action.setContent(c.getString(4));
				action.setEnergy(c.getString(5));
				action.setPlanTime(c.getString(6));
				action.setNeedTime(c.getString(7));
				action.setCompleted(Boolean.valueOf(c.getString(8)));
				actions.add(action);
				c.moveToNext();
			}
		} catch (SQLException e) {
			Log.e(e.toString());
		} finally {
			if (c != null && !c.isClosed()) {
				c.close();
			}
		}
		return actions;
	}

	@Override
	public void delete(int id) {
		db.delete(TABLE_NAME, "_id=" + id, null);		
	}

	@Override
	public void update(Action action) {
		ContentValues values = new ContentValues();
		values.put("content", action.getContent());
		values.put("date", action.getDate());
		values.put("detail", action.getDetail());
		values.put("importance", action.getImportance());
		values.put("context", action.getContent());
		values.put("energy", action.getEnergy());
		values.put("planTime", action.getPlanTime());
		values.put("needTime", action.getNeedTime());
		values.put("completed", action.getCompleted());
		db.update(TABLE_NAME, values, "_id=" + action.getId(), null);		
	}
	
	@Override
	public Action findById(int id) {
		Action action = null;
		Cursor c = null;
		try {
			c = db.query(TABLE_NAME, COLS, "_id=" + id, null, null, null, null);
			if (c.getCount() > 0) {
				c.moveToFirst();
				action = new Action();
				action.setId(c.getInt(0));
				action.setContent(c.getString(1));
				action.setDate(c.getString(2));
				action.setImportance(c.getString(3));
				action.setContent(c.getString(4));
				action.setEnergy(c.getString(5));
				action.setPlanTime(c.getString(6));
				action.setNeedTime(c.getString(7));
				action.setCompleted(Boolean.valueOf(c.getString(8)));
			}
		} catch (Exception e) {
			Log.v(e.toString());
		} finally {
			if (c != null && !c.isClosed()) {
				c.close();
			}
		}
		return action;
	}
	
	@Override
	public Action findLast() {
		Action action = null;
		Cursor c = null;
		try {
			c = db.query(TABLE_NAME, COLS, "completed = 'true'", null, null, null, "_id ASC", "LIMIT 1");
			if (c.getCount() > 0) {
				c.moveToFirst();
				action = new Action();
				action.setId(c.getInt(0));
				action.setContent(c.getString(1));
				action.setDate(c.getString(2));
				action.setImportance(c.getString(3));
				action.setContent(c.getString(4));
				action.setEnergy(c.getString(5));
				action.setPlanTime(c.getString(6));
				action.setNeedTime(c.getString(7));
				action.setCompleted(Boolean.valueOf(c.getString(8)));
			}
		} catch (Exception e) {
			Log.v(e.toString());
		} finally {
			if (c != null && !c.isClosed()) {
				c.close();
			}
		}
		return action;
	}
}
